package com.aerozhonghuan.logisticsmall.modules.mine.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.user.logic.UserInfoManager;
import com.aerozhonghuan.logisticsmall.utils.FileSizeUtil;
import com.aerozhonghuan.logisticsmall.utils.LocalCache;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;

import java.io.File;

import static java.util.ResourceBundle.clearCache;

/**
 * 描述 设置页面
 *
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class SettingFragment extends TitlebarFragment {

    private View rootView;
    private TextView tvCache;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_me_setting, null);
            initView();
            initData();
        }
        return rootView;
    }

    private void initView() {
        rootView.findViewById(R.id.btn_logout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserInfoManager.logout(getActivity());
            }
        });
        tvCache = (TextView) rootView.findViewById(R.id.tv_cache);
        rootView.findViewById(R.id.rel_cache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearCache();
            }
        });
    }

    private void initData() {
        double size = FileSizeUtil.getFileOrFilesSize(LocalCache.getCacheDir().getAbsolutePath(), FileSizeUtil.SIZETYPE_MB);
        tvCache.setText(String.format("%sM", size));
    }

    private void clearCache() {
        LocalCache.clearCacheDir(new LocalCache.OnClearCacheCallback() {
            @Override
            public void onSuccess() {
                alert("清除成功");
                initData();
            }

            @Override
            public void onFailure() {
                alert("清除失败");
            }
        });

    }
}
